﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows;
using System.Windows.Forms;
//#JMJ Create_20231107

namespace IHateWorking.Model
{
    /// <summary>
    /// 可订阅的状态变量
    /// </summary>
    public class SubState : INotifyPropertyChanged
    {
        public SubState(string name)
        {
            _name = name;
            _stateMode = 0;
        }

        public SubState(string name, bool mode)
        {
            _name = name;
            _stateMode = mode ? 1 : 0;
        }

        public SubState(string name, int mode)
        {
            _name = name;
            _stateMode = mode;
        }

        //变量的通知机制
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }

        //变量的订阅方法
        public delegate void ValueChangingHandle(object oldValue = null, object newValue = null);
        public event ValueChangingHandle ValueChanging = null;
        private void OnValueChanging(object oldValue = null, object newValue = null)
        { ValueChanging?.Invoke(oldValue, newValue); }

        public delegate void ValueChangedHandle();
        public event ValueChangedHandle ValueChanged = null;
        private void OnValueChanged()
        { ValueChanged?.Invoke(); }

        private string _name;
        /// <summary>
        /// 变量名
        /// </summary>
        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged();
                }
            }
        }

        private object _tag = null;
        /// <summary>
        /// 变量 Tag
        /// </summary>
        public object Tag
        {
            get => _tag;
            set
            {
                if (_tag != value)
                {
                    _tag = value;
                    OnPropertyChanged();
                }
            }
        }

        private int _stateMode;
        /// <summary>
        /// 模式
        /// </summary>
        public int StateMode
        {
            get => _stateMode;
            set
            {
                if (_stateMode != value)
                {
                    OnValueChanging(_stateMode, value);
                    _stateMode = value;
                    OnValueChanged();
                    OnPropertyChanged();
                }
            }
        }

        private int _limitMode = 1;
        /// <summary>
        /// 【尚未实装】上限号，当 StateMode 超出该值时需要手动清零
        /// </summary>
        public int LimitMode
        {
            get => _limitMode;
            set
            {
                if (_limitMode != value)
                {
                    _limitMode = value;
                    OnPropertyChanged();
                }
            }
        }
    }
}
